'use strict';

import Vue from 'vue'
import util from '@/util/Util'
import qs from 'qs'

const _vue = new Vue()

const _category = {
	// 增加分类
	'addCategory': function(param) {
		return _vue.$axios.post(util.getServerUrl('/category/addCategory'), {
				'id': param.id,
				'name': param.name,
				'parentId': param.parentId,
			})
			.then(function(response) {
				if(param.success) {
					param.success(response)
				}
				return response
			})
			.catch(function(error) {
				console.log(error)
			})
	},
	// 获取分类
	'getCategory': function(param) {
		let queryStr = '?1=1';
		if(param.id) {
			queryStr += '&id=' + param.id;
		}
		if(param.name) {
			queryStr += '&name=' + param.name;
		}
		if(param.parentId || param.parentId === 0) {
			queryStr += '&parentId=' + param.parentId;
		}
		return _vue.$axios.get(util.getServerUrl('/category/getCategory') + queryStr)
			.then(function(response) {
				if(param.success) {
					param.success(response)
				}
				return response
			})
			.catch(function(error) {
				console.log(error)
			})
	},
	// 获取热门分类
	'getHotCategory': function(param) {
		return _vue.$axios.get(util.getServerUrl('/category/getHotCategory'))
			.then(function(response) {
				if(param.success) {
					param.success(response)
				}
				return response
			})
			.catch(function(error) {
				console.log(error)
			})
	},
	// 更新分类
	'updateCategory': function(param) {
		return _vue.$axios.post(util.getServerUrl('/category/updateCategory'), {
				'id': param.id,
				'name': param.name,
				'parentId': param.parentId,
			})
			.then(function(response) {
				if(param.success) {
					param.success(response)
				}
				return response
			})
			.catch(function(error) {
				console.log(error)
			})
	},
	// 删除分类
	'deleteCategory': function(param) {
		return _vue.$axios.post(util.getServerUrl('/category/deleteCategory'), {
				'id': param.id,
			})
			.then(function(response) {
				if(param.success) {
					param.success(response)
				}
				return response
			})
			.catch(function(error) {
				console.log(error)
			})
	},
}

export default _category